import { createRouter, createWebHistory } from "vue-router";
import { getToken } from "../utils/token-utils";
import AddPet from "../pages/AddPet.vue";
import ProductDetail from '../pages/ProductDetail.vue';
import CheckOrder from '../pages/CheckOrder.vue';
import Address from '../pages/Address.vue';
import AddressDetail from '../pages/AddressDetail.vue';
import UpdateAddress from '../pages/UpdateAddress.vue';
import PetShop from '../pages/PetShop.vue'
import SeekPublish from "../pages/SeekPublish.vue";
import Menu from "../pages/PublishMenu.vue";
import SeekDetail from "../pages/SeekDetail.vue";
import Seeking from "../pages/Seeking.vue";
import Login from "../pages/Login.vue";
import LoginByCode from "../pages/LoginByCode.vue";
import Register from "../pages/Register.vue";
import PetsTabbar from "../components/PetsTabbar.vue";
import LineTogic from "../pages/LineTogic.vue";
import PetsHeader from "../components/PetsHeader.vue";
import TopicDetail from "../pages/TopicDetail.vue";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "lineTogic",
    },

    {
      name: "lineTogic",
      path: "/lineTogic",
      component: LineTogic,
    },
    {
      name: "topicDetail",
      path: "/topicDetail",
      component: TopicDetail,
    },
    {
      name: "login",
      path: "/login",
      component: Login,
    },
    {
      name: "register",
      path: "/register",
      component: Register,
    },
    {
      name: "loginByCode",
      path: "/loginByCode",
      component: LoginByCode,
    },
    {
      name: "petsHeader",
      path: "/petsHeader",
      component: PetsHeader,
    },
    {
      name: "petsTabbar",
      path: "/petsTabbar",
      component: PetsTabbar,
    },
    {
      name: "addPet",
      path: "/addPet",
      component: AddPet,
      meta: { requiresAuth: true }
    },
    {
      name: "productDetail",
      path: "/productDetail",
      component: ProductDetail,
      
    },
    {
      name: "checkOrder",
      path: "/checkOrder",
      component: CheckOrder,
      meta: { requiresAuth: true }
    },
    {
      name: "address",
      path: "/address",
      component: Address,
    },
    {
      name: "addressDetail",
      path: "/addressDetail",
      component: AddressDetail,
    },
    {
      name: "updateAddress",
      path: "/updateAddress",
      component: UpdateAddress,
    },
    {
      name: "petShop",
      path: "/petShop",
      component: PetShop,
    },

    {
      name: "seekPublish",
      path: "/seekPublish",
      component: SeekPublish,
      meta: { requiresAuth: true }
    },
    {
      name: "menu",
      path: "/menu",
      component: Menu,
    },
    {
      name: "seekDetail",
      path: "/seekDetail",
      component: SeekDetail,
    },
    {
      name: "seeking",
      path: "/seeking",
      component: Seeking,
    },
  ],
});

//全局前置守卫
// router.beforeEach((to, from, next) => {
//   const token = getToken();
//   if (token) {
//     if (to.path == "/login" || to.path == "/register") {
//       next({ path: "/" });
//     } else {
//       next();
//     }
//   } else {
//     next();
//   }
// });
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !localStorage.getItem('vue_login_token')) {
    next('/login');
  } else {
    next();
  }
});

//全局后置守卫

export default router;
